package cn.com.fone.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;


/**
 * 调用路由之前被调用
 */
public class PreRequestLogFilter extends ZuulFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(PreRequestLogFilter.class);
    private static final String FILTER_TYPE = "pre";

    @Override
    public String filterType() {
        // 返回拦截器类型
        return FILTER_TYPE;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        // 打印出请求地址
        LOGGER.info(String.format("send %s request to %s ", request.getMethod(), request.getRequestURI().toString()));
        return null;
    }
}
